package com.coderman.business.service.imp;

import com.coderman.business.mapper.PayMentMapper;
import com.coderman.business.service.IPayMentService;
import com.coderman.business.service.IStudentService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.model.business.ClassInfo;
import com.coderman.common.model.payMent.PayMent;
import com.coderman.common.vo.business.PayMentVo;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
public class PayMentServiceImpl  implements IPayMentService {

    @Autowired
    private PayMentMapper payMentMapper;
    @Autowired
    private IStudentService studentService;
    @Override
    public PageVO<PayMent> findList(Integer pageNum, Integer pageSize, PayMentVo payMentVo) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(PayMent.class);
        Integer paymentid = payMentVo.getPaymentid();
        Integer isperiodization = payMentVo.getIsperiodization();
        Integer studentid = payMentVo.getStudentid();
        Integer paymentmode = payMentVo.getPaymentmode();

        Example.Criteria criteria = o.createCriteria();
        o.setOrderByClause("paymenttime desc");
        if(paymentid !=null){
            criteria.andLike("paymentid","%"+paymentid+"%");
        }
        if(isperiodization!=null){
            criteria.andEqualTo("isperiodization",isperiodization);
        }
        if(paymentmode!=null){
            criteria.andEqualTo("paymentmode",paymentmode);
        }
        if(studentid!=null){
            criteria.andEqualTo("studentid",studentid);
        }
        if(payMentVo.getStartTime()!=null){
            criteria.andGreaterThanOrEqualTo("paymenttime",payMentVo.getStartTime());
        }
        if(payMentVo.getEndTime()!=null){
            criteria.andLessThanOrEqualTo("paymenttime",payMentVo.getEndTime());
        }
        List<PayMent> payMents= payMentMapper.selectByExample(o);
        payMents.stream().forEach(payMent -> {
            payMent.setStudentname(studentService.getStuNameById(payMent.getStudentid()));
        });
        PageInfo<PayMent> info=new PageInfo<>(payMents);
        return new PageVO<>(info.getTotal(),payMents);
    }

    @Override
    public boolean add(PayMent payMent) {
        return payMentMapper.insert(payMent)>0;
    }

    @Override
    public PayMent getById(Integer paymentid) {
        return payMentMapper.selectByPrimaryKey(paymentid);
    }

    @Override
    public void deleteById(Integer paymentid) {
          payMentMapper.deleteByPrimaryKey(paymentid);
    }

    @Override
    public void update(Integer paymentid, PayMent payMent) {
        payMentMapper.updateByPrimaryKey(payMent);
    }

    @Override
    public List<PayMent> findAll() {
        return payMentMapper.selectAll();
    }
}
